package org.southyin.springboot02.service.impl;

import org.southyin.springboot02.commons.CommonsResp;
import org.southyin.springboot02.mapper.CmsCustomerMapper;
import org.southyin.springboot02.model.CmsCustomer;
import org.southyin.springboot02.service.CmsCustomerService;
import org.southyin.springboot02.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CmsCustomerServiceImpl implements CmsCustomerService {
    @Autowired
    CmsCustomerMapper dao;
    public CommonsResp customerReg(CmsCustomer cmsCustomer) {
        // 查询 注册用户的邮箱是否被占用
        int count = dao.findCustomerByEmail(cmsCustomer.getEmail());
        if (count == 0) {
            // 做注册
            cmsCustomer.setPassword(MD5.getMd5(cmsCustomer.getPassword()+cmsCustomer.getEmail())); // 加密
            int i = dao.saveCustomer(cmsCustomer);
            if (i != 0) {
                return CommonsResp.succeess(200,"新增用户成功");
            }
        }
        return CommonsResp.succeess(500,"用户邮箱被占用");
    }

    public CommonsResp customerLogin(CmsCustomer cmsCustomer) {
        cmsCustomer.setPassword(MD5.getMd5(cmsCustomer.getPassword()+cmsCustomer.getEmail())); // 加密
        CmsCustomer customer = dao.findCustomerByEmailAndPass(cmsCustomer);
        if (customer == null) {
            return CommonsResp.failed(500, "用户邮箱或密码错误");
        }
        if (!customer.getEnabled()) {
            return CommonsResp.failed(500, "用户被禁用");
        }
        if (!customer.getDelMark()) {
            return CommonsResp.failed(500, "用户被锁定");
        }
        customer.setPassword(null);
        return CommonsResp.succeess(200, "登录成功",customer);
    }

    public CmsCustomer getPersonInfos(Long id) {
        return dao.getPersonInfos(id);
    }
}
